Skip to content

BridgeJS: Fix issues exporting nested types#783

Merged
krodak merged 3 commits into
swiftwasm:mainfrom
wfltaylor:js-merge-structs
Jul 1, 2026
Merged

BridgeJS: Fix issues exporting nested types#783
krodak merged 3 commits into
swiftwasm:mainfrom
wfltaylor:js-merge-structs

Conversation

@wfltaylor

Copy link
Copy Markdown
Contributor

There are currently a few issues with the JS/TS code generation, due to a lack of merging. For example, the following Swift code:

@JS struct Control {
    @JS enum Variant: String { case button, slider }

    @JS struct Layout {
        @JS enum Alignment: String { case leading, trailing }
        var padding: Int
    }

    var name: String

    @JS init(name: String) { self.name = name }
}

generates a .d.ts:

export type Exports = {
    Control: {
        init(name: string): Control;
    },
    Control: {
        Variant: VariantObject,
        Layout: {
            Alignment: AlignmentObject
        }
    }
}

This doesn’t type check correctly. Similar issues occur with the emitted .js, except instead of failing to type check, these will cause runtime issues (since keys are overwritten instead of merged).

This PR is a bit of a refactor of BridgeJSLink to address this. Instead of being a member of a NamespaceNode, each class and struct becomes its own NamespaceNode. This makes it much easier to merge everything together so it can be emitted as one unit.

@kateinoigakukun kateinoigakukun requested a review from krodak June 30, 2026 06:41

@krodak krodak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling this, @wfltaylor, one thing to address, I'll get back to you on it soon, good otherwise 👌🏻

@krodak krodak self-requested a review July 1, 2026 21:51

@krodak krodak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed, looks good, merging the nested types this way is clean. 👍

Heads up: I pushed 2 commits on top. They're not strictly part of your change, but your new fixtures surfaced a pre-existing bug, so I folded them in:

  • Fix static property call expression for nested structs/enums (Outer_Inner.propOuter.Inner.prop; the .classStatic case was already fixed this way, just extended it to struct/enum)`.
  • Runtime coverage for the above via NestedTypeHost.

@krodak krodak enabled auto-merge July 1, 2026 21:53
@krodak krodak merged commit 8c08980 into swiftwasm:main Jul 1, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants